<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 19-4-9
 * Time: 下午4:16
 */

namespace api\modules\nbrb\controllers;


use common\components\huaxiayinhang\Nbrb;
use common\models\NbrbFinalAuditApplyBack;
use common\models\NbrbLoanApplyBack;
use common\models\NbrbUnderwriteApplyBack;
use wodrow\yii\rest\Controller;

class NotifyController extends Controller
{
    public function beforeAction($action)
    {
        $raw = \Yii::$app->request->rawBody;
        \Yii::$app->request->setRawBody(urldecode($raw));
        return parent::beforeAction($action);
    }

    /**
     * 4.核保申请通知
     * @get/post
     * @throws
     */
    public function actionUnderwriteApply()
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
//        unset(\Yii::$app->request->parsers['application/json']);
        $json = \Yii::$app->request->rawBody;
        $data = json_decode($json, true);
        $nbrb = new Nbrb();
        $data['infoContent'] = json_decode($nbrb->decryptInfoContent($data['infoContent']), true);
        $back = new NbrbUnderwriteApplyBack();
        $back->request_seq_no = $data['requestSeqNo'];
        $back->item_code = $data['itemCode'];
        $back->company_code = $data['companyCode'];
        $back->interface_code = $data['interfaceCode'];
        $back->interface_version = $data['interfaceVersion'];
        $back->under_write_number = $data['infoContent']['underWriteNumber'];
        $back->audit_result = $data['infoContent']['aduitResult'];
        $back->audit_opinion = $data['infoContent']['aduitOpinion'];
        $back->risk_rate = isset($data['infoContent']['riskRate'])?$data['infoContent']['riskRate']:null;
        $back->max_loan_amt = isset($data['infoContent']['maxLoanAmt'])?$data['infoContent']['maxLoanAmt']:null;
        $back->req_data = $json;
        $back->created_at = time();
        $back->resp_info_data = '{"success":"Y"}';
        if (!$back->validate()){
            $back->resp_info_data = '{"success":"N"}';
            $mapData = [
                'respCode' => "N",
                'respMsg' => "失败",
            ];
        }else{
            $mapData = [
                'respCode' => "S",
                'respMsg' => "成功",
            ];
        }
        $map = json_decode($back->resp_info_data, true);
        $now = time();
        $nbrb->req_log->request_interface_code = 'UnderWriteAuditNotice';
        $nbrb->req_log->request_interface_version = "v1";
        $nbrb->encryptAndSign($mapData, $map, $now);
        $back->resp_data = $mapData;
        $back->save();
        echo $back->resp_data;
        exit;
    }

    /**
     * 8.终审结果通知接口
     * @get/post
     * @throws
     */
    public function actionFinalAuditNotice()
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $json = \Yii::$app->request->rawBody;
        $data = json_decode($json, true);
        $nbrb = new Nbrb();
        $data['infoContent'] = json_decode($nbrb->decryptInfoContent($data['infoContent']), true);
        $back = new NbrbFinalAuditApplyBack();
        $back->request_seq_no = $data['requestSeqNo'];
        $back->item_code = $data['itemCode'];
        $back->company_code = $data['companyCode'];
        $back->interface_code = $data['interfaceCode'];
        $back->interface_version = $data['interfaceVersion'];
        $back->final_audit_number = $data['infoContent']['finalAduitNumber'];
        $back->audit_result = $data['infoContent']['aduitResult'];
        $back->audit_opinion = $data['infoContent']['aduitOpinion'];
        $back->risk_rate = isset($data['infoContent']['riskRate'])?$data['infoContent']['riskRate']:null;
        $back->max_loan_amt = isset($data['infoContent']['maxLoanAmt'])?$data['infoContent']['maxLoanAmt']:null;
        $back->req_data = $json;
        $back->created_at = time();
        $back->resp_info_data = '{"success":"Y"}';
        if (!$back->validate()){
            $back->resp_info_data = '{"success":"N"}';
            $mapData = [
                'respCode' => "N",
                'respMsg' => "失败",
            ];
        }else{
            $mapData = [
                'respCode' => "S",
                'respMsg' => "成功",
            ];
        }
        $map = json_decode($back->resp_info_data, true);
        $now = time();
        $nbrb->req_log->request_interface_code = 'FinalAuditNotice';
        $nbrb->req_log->request_interface_version = "v1";
        $nbrb->encryptAndSign($mapData, $map, $now);
        $back->resp_data = $mapData;
        $back->save();
        echo $back->resp_data;
        exit;
    }

    /**
     * 10.放款申请审核结果通知接口
     * @get/post
     * @throws
     */
    public function actionLoanApplyAduitResult()
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $json = \Yii::$app->request->rawBody;
        $data = json_decode($json, true);
        $nbrb = new Nbrb();
        $data['infoContent'] = json_decode($nbrb->decryptInfoContent($data['infoContent']), true);
        $back = new NbrbLoanApplyBack();
        $back->request_seq_no = $data['requestSeqNo'];
        $back->item_code = $data['itemCode'];
        $back->company_code = $data['companyCode'];
        $back->interface_code = $data['interfaceCode'];
        $back->interface_version = $data['interfaceVersion'];
        $back->loan_apply_number = $data['infoContent']['loanApplyNumber'];
        $back->audit_result = $data['infoContent']['aduitResult'];
        $back->audit_opinion = $data['infoContent']['aduitOpinion'];
        $back->risk_rate = isset($data['infoContent']['riskRate'])?$data['infoContent']['riskRate']:null;
        $back->max_loan_amt = isset($data['infoContent']['maxLoanAmt'])?$data['infoContent']['maxLoanAmt']:null;
        $back->req_data = $json;
        $back->created_at = time();
        $back->resp_info_data = '{"success":"Y"}';
        if (!$back->validate()){
            $back->resp_info_data = '{"success":"N"}';
            $mapData = [
                'respCode' => "N",
                'respMsg' => "失败",
            ];
        }else{
            $mapData = [
                'respCode' => "S",
                'respMsg' => "成功",
            ];
        }
        $map = json_decode($back->resp_info_data, true);
        $now = time();
        $nbrb->req_log->request_interface_code = 'LoanApplyAduitResult';
        $nbrb->req_log->request_interface_version = "v1";
        $nbrb->encryptAndSign($mapData, $map, $now);
        $back->resp_data = $mapData;
        $back->save();
        echo $back->resp_data;
        exit;
    }
}